package com.ma.imserver.netty;

import com.ma.common.contant.IMRedisKey;
import com.ma.common.mq.RedisMQTemplate;
import jakarta.annotation.PreDestroy;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@AllArgsConstructor
public class IMServerGroup implements CommandLineRunner {
    public static volatile long serverId =0;
    private final RedisMQTemplate redisMQTemplate;
    private final List<IMServer> imServers;

    /**
     * 判断服务器是否就绪
     * @param args
     * @throws Exception
     */
    public boolean isReady(){
        for (IMServer imServer : imServers) {
            if (!imServer.isReady()){
                return false;
            }
        }
        return true;
    }
    @Override
    public void run(String... args) throws Exception {
        //初始化SERVER_ID
        String key = IMRedisKey.IM_MAX_SERVER_ID;
        serverId = redisMQTemplate.opsForValue().increment(key,1);
        //启动服务
        for (IMServer imServer : imServers) {
            imServer.start();
        }
    }
    @PreDestroy
    public void destory(){
        //停止服务
        for (IMServer imServer : imServers) {
            imServer.stop();
        }
    }
}
